package com.movieproject.src;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.movieproject.daos.MovieDAO;
import com.movieproject.dataobjects.Movie;

@Controller
@Transactional
public class SearchController {
	@Autowired 
	private MovieDAO movieDAO;
	
	@RequestMapping(value = "browse", method = RequestMethod.GET)
	public ModelAndView Filter(
		@RequestParam(value = "genre", required = false) String genre,
		@RequestParam(value = "rating", required = false) String rating,
		@RequestParam(value = "decade", required = false) String year) {
		ModelAndView m = new ModelAndView();
		Map<String, ArrayList<Movie>> moviesByGenre = new HashMap<String, ArrayList<Movie>>();
		ArrayList<String> genres = (ArrayList<String>) movieDAO.getGenres();
		for(String g : genres) {
		    moviesByGenre.put(g, (ArrayList<Movie>) movieDAO.getMoviesByGenre(g));
		}
		ArrayList<Movie> filterResults = (ArrayList<Movie>) movieDAO.filterMovies(genre, year, rating);
		m.getModelMap().addAttribute("moviesbygenre", moviesByGenre);
		m.getModelMap().addAttribute("filterresults", filterResults);
		m.getModelMap().addAttribute("genredropdown", getGenreDropDown());
		m.getModelMap().addAttribute("decadedropdown", getDecadeDropDown());
		m.getModelMap().addAttribute("ratingdropdown", getRatingDropDown());
		return m;
		}
	
	@RequestMapping(value = "searchalt", method = RequestMethod.GET)
	public ModelAndView Search(@RequestParam(value = "searchBox", required = true) String searchString) {
	    ModelAndView m = new ModelAndView();
	    ArrayList<Movie> searchResults = (ArrayList<Movie>) movieDAO.searchMovies(searchString);
	    m.getModelMap().addAttribute("searchresults", searchResults);
	    return m;
	}
	
	private Map<String, String> getGenreDropDown() {
	    Map<String, String> dd = new LinkedHashMap<String, String>();
	    for(String genre : movieDAO.getGenres()) {
		dd.put(genre, genre);
	    }
	    return dd;
	}
	
	private Map<String, String> getDecadeDropDown() {
	    Map<String, String> dd = new LinkedHashMap<String, String>();
	    dd.put("%", "All Years");
	    dd.put("197%", "1970s");
	    dd.put("198%", "1980s");
	    dd.put("199%", "1990s");
	    dd.put("200%", "2000s");
	    dd.put("201%", "2010s");
	    return dd;
	}
	
	private Map<String, String> getRatingDropDown() {
	    Map<String, String> dd = new LinkedHashMap<String, String>();
	    dd.put("%", "All Ratings");
	    for(String rating : movieDAO.getRatings()) {
		dd.put(rating, rating);
	    }
	    return dd;
	}
}
